package com.user.read.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.user.read.dto.UserDTO;
import com.user.read.service.UserService;
import com.user.read.util.PageRequest;
import com.user.read.util.PageResult;
import com.user.read.vo.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ClassName UserReadController
 * @Desecription TODO 1.2新增
 * @Author gjy
 * @Date 2021/3/19 12:42
 * @Version 1.2
 **/
@RefreshScope
@RestController
@RequestMapping("/user")
public class UserReadController {

    private static final Logger log = LoggerFactory.getLogger(UserReadController.class);
    @Autowired
    private UserService userService;

    @Value("${project.name:}")
    private String projectName;

    @GetMapping("/get")
    public UserDTO get(@RequestParam("id") int id) {
        System.out.println(projectName);
        return userService.getOne(id);
    }

    @GetMapping("/getAll")
    public ResultData getAll() {
        System.out.println(projectName);
        ResultData resultData = new ResultData("200","ok");
        try {
            List<UserDTO> userDTOList =userService.getAll();
            //int i = 1/0;
            resultData.setData(userDTOList);
        } catch (Exception e) {
            log.error(e.getMessage());
            resultData.setMessage("error");
            resultData.setCode("-1");
            return resultData;
        }
        return resultData;
    }

    @GetMapping("/excelPort")
    public void download(HttpServletResponse response) throws IOException {
        long start = System.currentTimeMillis();
        List<UserDTO> list = userService.getAll();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("学生信息导出表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UserDTO.class).sheet("模板").registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).doWrite(list);
        long end = System.currentTimeMillis();
        log.info("导出耗时{}",end-start);
    }


    @PostMapping(value="/findPage")
    public ResultData findPage(@RequestBody PageRequest pageQuery) {

        ResultData resultData = new ResultData("200","ok");
        try {
            PageResult page = userService.findPage(pageQuery);
            //int i = 1/0;
            resultData.setData(page);
            resultData.setVersion("1.0");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultData.setMessage("error");
            resultData.setCode("-1");
            return resultData;
        }
        return resultData;
    }
}
